package com.sfx.controller;

import com.sfx.domain.ResponseResult;
import com.sfx.domain.dto.LoginDto;
import com.sfx.domain.entity.User;
import com.sfx.domain.vo.AdminInfoVO;
import com.sfx.enums.AppHttpCodeEnum;
import com.sfx.exception.SystemException;
import com.sfx.service.AdminLoginService;
import com.sfx.utils.BeanCopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-14
 * Time: 17:57
 */
@RestController
public class AdminLoginController {

    @Autowired
    private AdminLoginService adminLoginService;

    /**
     * 登陆功能
     * @return
     */
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody LoginDto userDto) {
        //参数校验
        User user = BeanCopyUtils.copyBean(userDto, User.class);
        String userName = user.getUserName();
        String password = user.getPassword();
        if(StringUtils.isAnyBlank(userName,password)) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME_PASSWORD);
        }
        return adminLoginService.login(user);
    }

    @GetMapping("/getInfo")
    public ResponseResult<AdminInfoVO> getInfo() {
        return adminLoginService.getInfo();
    }

    @GetMapping("/getRouters")
    public ResponseResult getRouters() {
        return adminLoginService.getRouters();
    }

}